package com.zhentao.service.Impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.user.dto.LoginDto;
import com.zhentao.model.dtos.ResponseResult;
import com.zhentao.model.enums.AppHttpCodeEnum;
import com.zhentao.model.user.mapper.ApUserMapper;
import com.zhentao.model.user.pojo.ApUser;
import com.zhentao.service.ApuserService;
import com.zhentao.utils.common.AppJwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class ApuserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApuserService {


    @Autowired
    private ApUserMapper apUserMapper;



    /**
     * 登录
     */
    @Override
    public ResponseResult login(LoginDto loginDto) {

        //判断账号和密码是否为空
        if (StringUtils.isBlank(loginDto.getPhone()) && StringUtils.isBlank(loginDto.getPassword())) {
            LambdaQueryWrapper<ApUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ApUser::getPhone, loginDto.getPhone());

            //进行检验
            ApUser apUser = apUserMapper.selectOne(lambdaQueryWrapper);

            //账号不存在
            if (apUser == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "账号不存在");
            }

            //密码错误
            String salt = apUser.getSalt();
            String password = DigestUtils.md5DigestAsHex((loginDto.getPassword() + salt).getBytes());
            if (!password.equals(apUser.getPassword())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR, "密码错误");
            }

            //账号密码都正确  把数据返回给前端 把对象通过jwt加密-》token-->前端
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("token", AppJwtUtil.getToken(apUser.getId().longValue()));

            //盐和密码不能传递给前端
            apUser.setPassword(null);
            apUser.setSalt(null);

            map.put("user", apUser);
            return ResponseResult.okResult(map);


        } else {
            //游客登录 默认为0;
            Map<String, Object> map = new HashMap<>();
            map.put("token", AppJwtUtil.getToken(0L));


            return ResponseResult.okResult(map);

        }

    }









    /**
     * 注册
     */
    @Override
    public ResponseResult register(ApUser apUser) {
        //判断手机号和密码是否为空
        if (StringUtils.isNotBlank(apUser.getPhone()) && StringUtils.isNotBlank(apUser.getPassword())) {
            LambdaQueryWrapper<ApUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ApUser::getPhone, apUser.getPhone());
            ApUser apUser1 = apUserMapper.selectOne(lambdaQueryWrapper);
            //判断账号是否已存在
            if (apUser1 != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "账号已存在");
            }

            UUID uuid = UUID.randomUUID();
            String s = uuid.toString();

            String salt = apUser.getSalt().substring(Integer.parseInt(s), 36);
            String pass = DigestUtils.md5DigestAsHex((apUser.getPassword() + salt).getBytes());

//            用BCryptPasswordEncoder进行密码加密
//            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//            String salt = passwordEncoder.generateSalt();
//            String pass = passwordEncoder.encode(apUser.getPassword());

            apUser.setSalt(salt);
            apUser.setPassword(pass);
            apUser.setCreatedTime(new Date());

            int insert = apUserMapper.insert(apUser);
            if (insert > 0) {
                return ResponseResult.okResult("注册成功");
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "注册失败");
    }
}
